package com.atzly.community.controller.interceptor;

import com.atzly.community.entity.User;
import com.atzly.community.service.MessageService;
import com.atzly.community.utils.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**   处理用户的未读消息显示
 * @author zlysimida
 * @date 2021/9/1 - 16:10
 */
@Component
public class MessageInterceptor implements HandlerInterceptor {
    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private MessageService messageService;

    // 模板返回之前添加全部未读数量
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        User user = hostHolder.getUserFromThread();
        if(user != null && modelAndView != null){
            int noReadLetterCount = messageService.getNoReadLetterCount(user.getId(), null);
            int noReadNotifyCount = messageService.selectSystemNoReadNotify(user.getId(), null);
            modelAndView.addObject("allNoReadCount",noReadNotifyCount + noReadLetterCount);
        }
    }
}
